package BlockR.function;

import Demo.DemoForCpabe;

import java.io.*;

/**
 * @author spencercjh
 */
public class Output {
    public static String charsetName = DemoForCpabe.charsetName;

    public static void outputContent(String outputPath, byte[][][] allBlock) {
        StringBuilder output = new StringBuilder();
        for (byte[][] indexBlock : allBlock) {
            if (indexBlock != null) {
                for (byte[] indexPiece : indexBlock) {
                    if (indexPiece != null) {
                        try {
                            output.append(new String(indexPiece, charsetName));
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    } else {
                        break;
                    }
                }
            }
        }
        try {
            OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(outputPath), charsetName);
            out.append(output);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("输出密文文件失败");
        }
    }

    public static void output(String outputPath, byte[][][] allBlock) {
        boolean flagExit = true;
        try {
            OutputStream out = new FileOutputStream(outputPath);
            for (byte[][] indexBlock : allBlock) {
                if (indexBlock != null) {
                    for (byte[] indexPiece : indexBlock) {
                        if (indexPiece != null) {
                            flagExit = true;
                            for (byte indexByte : indexPiece) {
                                if (indexByte != 0) {
                                    flagExit = false;
                                }
                            }
                            if (flagExit) {
                                break;
                            } else {
                                out.write(indexPiece);
                            }
                        } else {
                            break;
                        }
                    }
                    if (flagExit) {
                        break;
                    }
                } else {
                    break;
                }
            }
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("输出文件失败");
        }
    }

}
